﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Framework.FSM
{
    /// <summary>
    /// 状态机管理类
    /// </summary>
    public class FsmManager : IFsmManager, IModule
    {
        public bool m_bOpenAuxillaryConnection = false; // 是否开启辅助并线功能
        private Dictionary<string, FSM> m_Fsms = null;


        public IFSM CreateFsm(object owner, string fsmName, List<IFsmState> states)
        {
            if (string.IsNullOrEmpty(fsmName)) return null;
            FSM fsm = null;
            if(m_Fsms.TryGetValue(fsmName, out fsm))
            {
                return fsm;
            }
            fsm = new FSM();
            fsm.InitFsm(owner, fsmName, states);
            m_Fsms.Add(fsmName, fsm);
            return fsm;
        }
        public void DestroyFsm(string fsmName)
        {
            if (string.IsNullOrEmpty(fsmName)) return;
            FSM fsm = null;
            if(m_Fsms.TryGetValue(fsmName, out fsm))
            {
                fsm.DestroyFsm();
                m_Fsms.Remove(fsmName);
            }
        }
        public void GetAllFsm(ref List<IFSM> list)
        {
            if (list == null) return;
            foreach (var item in m_Fsms)
            {
                list.Add(item.Value);
            }
        }
        public IFSM GetFsm(string fsmName)
        {
            if (string.IsNullOrEmpty(fsmName)) return null;
            FSM fsm = null;
            m_Fsms.TryGetValue(fsmName, out fsm);
            return fsm;
        }


        public bool HasFsm(string fsmName)
        {
            if (string.IsNullOrEmpty(fsmName)) return false;
            return m_Fsms.ContainsKey(fsmName);
        }




        /// <summary>
        /// 模块优先级
        /// </summary>

        public bool GetOpenAuxillaryConnection()
        {
            return  m_bOpenAuxillaryConnection;
        }

        public void SetOpenAuxillaryConnection(bool bOpenAuxillary)
        {
            m_bOpenAuxillaryConnection = bOpenAuxillary;
            //Debug.LogError("=m_bOpenAuxillaryConnection=" + m_bOpenAuxillaryConnection);
        }

        public int Priority
        {
            get
            {
                return ModulePriority.FSMPriority;
            }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            m_Fsms = new Dictionary<string, FSM>();
        }
        /// <summary>
        /// 销毁
        /// </summary>
        public void Destroy()
        {
            foreach (var item in m_Fsms)
            {
                item.Value.DestroyFsm();
            }
            m_Fsms.Clear();
        }
        /// <summary>
        /// 更新函数
        /// </summary>
        /// <param name="frameTime">逻辑一帧时间</param>
        /// <param name="realTime">实际一帧时间</param>
        public void Update(float frameTime, float realTime)
        {

            foreach (var item in m_Fsms)
            {
                if (item.Value == null)
                    Debug.LogError( "item == "  + item.Value.ToString() );
                item.Value.Update(frameTime, realTime);
            }
        }
        /// <summary>
        /// 重置接口
        /// </summary>
        public void Reset()
        {
            foreach (var item in m_Fsms)
            {
                item.Value.DestroyFsm();
            }
            m_Fsms.Clear();
        }
    }
}
